package com.glasses.business.base;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.pm.ActivityInfo;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.glasses.business.R;
import com.glasses.business.appliction.MyApplication;
import com.glasses.business.fmanager.StackManager;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.zxc.layout.autolayout.AutoLayoutActivity;


/**
 * 作者 : hjb
 * 时间 : 2016/4/25.
 */
public class BaseActivity extends AutoLayoutActivity {


    private BroadcastReceiver recv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 竖屏锁定
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setSystemBarTint();
    }

    @Override
    protected void onDestroy() {
        try {
            unregisterReceiver(recv);
        } catch (Exception e) {
        }
        super.onDestroy();
    }

    public void setTitleTv(int id) {
        TextView tv = (TextView) findViewById(R.id.title_tv);
        tv.setText(id);
    }

    private void back() {
        finish();
    }

    public void setTitleTv(String str) {
        TextView tv = (TextView) findViewById(R.id.title_tv);
        tv.setText(str);
    }

    public void setNextTv(String str) {
        TextView tv = (TextView) findViewById(R.id.next_tv);
        tv.setVisibility(View.VISIBLE);
        tv.setText(str);
        View iv = findViewById(R.id.next_img);
        iv.setVisibility(View.GONE);
    }

    public TextView getNextTv() {

        return (TextView) findViewById(R.id.next_tv);
    }

    public void setBackGone() {
        View tv = findViewById(R.id.ly_back);
        tv.setVisibility(View.GONE);
    }


    @SuppressLint("NewApi")
    public void setNextTvBG(Drawable drawable) {
        TextView tv = (TextView) findViewById(R.id.next_tv);
        tv.setBackground(drawable);
    }

    public void setNextImage(int id) {
        TextView tv = (TextView) findViewById(R.id.next_tv);
        tv.setVisibility(View.GONE);
        ImageView iv = (ImageView) findViewById(R.id.next_img);
        iv.setVisibility(View.VISIBLE);
        iv.setImageResource(id);
    }

    public void setNextOnClick(View.OnClickListener onClick) {
        TextView tv = (TextView) findViewById(R.id.next_tv);
        tv.setOnClickListener(onClick);
        View iv = findViewById(R.id.next_img);
        iv.setOnClickListener(onClick);
    }

    public void setNextGone() {
        TextView tv = (TextView) findViewById(R.id.next_tv);
        tv.setVisibility(View.GONE);
        ImageView iv = (ImageView) findViewById(R.id.next_img);
        iv.setVisibility(View.GONE);
    }

    /**
     * 设置沉浸式
     */
    public void setSystemBarTint() {
        //只对api19以上版本有效
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        //为状态栏着色
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(R.color.common_gray);
    }

    /**
     * 设置沉浸式
     */
    public void setSystemBarTint_() {
        //只对api19以上版本有效
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        //为状态栏着色
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(R.color.brown);
//        tintManager.setStatusBarTintResource(R.color.transparent);
    }

    /**
     * 设置沉浸式
     */
    public void setSystemBarTint_p() {
        //只对api19以上版本有效
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        //为状态栏着色
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setTintResource(R.mipmap.login);
//        tintManager.setStatusBarTintResource(R.color.brown);
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            StackManager manager = MyApplication.getInstance().manager;
            if (manager == null) {
                finish();
            } else {
                manager.onBackPressed();
            }
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    public void back(View view) {
        StackManager manager = MyApplication.getInstance().manager;
        if (manager == null) {
            finish();
        } else {
            manager.onBackPressed();
        }
    }

}